package com.example.springboot2.Service;

import com.example.springboot2.DB_Helper.JDBCUtils;
import com.example.springboot2.Model.Sys_Module;
import com.example.springboot2.Model.Sys_User;
import com.example.springboot2.ViewModel.subMenu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Sys_ModuleService {
    public List<Sys_Module> GetModuleByUser(Sys_User sys_user) throws SQLException, InstantiationException, IllegalAccessException {
        if(sys_user.Sys_IsAdmin.equals(1))  //超级管理员获取所有菜单
        {
            Connection conn = JDBCUtils.findConnection();
            PreparedStatement pre = null;
            ResultSet res = null;
            String sql = "select * from Sys_Module where 1=1 order by Sys_SortNo";
            pre = conn.prepareStatement(sql);
            res = pre.executeQuery();
            //调用将结果集转换成实体对象方法
            List<Sys_Module> list = JDBCUtils.Populate(res,Sys_Module.class);  //.populate(res, TestModel.class);
            //循环遍历结果
            for(int i=0;i<list.size();i++){
                Sys_Module model = (Sys_Module) list.get(i);
            }
            return  list;
        }
        else
        {
            return  null; //后期补上
        }
    }

    public List<subMenu> GetSubMenuList(String ModuleId,List<Sys_Module> ModuleList)
    {
        List<subMenu> Result=new ArrayList<>();
        List<Sys_Module> SubModuleList=ModuleList.stream().filter(x->x.Sys_ModuleParentId.equals(ModuleId)).collect(Collectors.toList());
        if(SubModuleList.size()>0)
        {
            for (Sys_Module module:
                    SubModuleList) {
                subMenu menu=new subMenu();
                menu.ModuleId=module.Sys_ModuleId;
                menu.url=module.Sys_ModuleUrl;
                menu.name= module.Sys_ModuleName;
                menu.subMenus=new ArrayList<>();
                GetSubMenuList(menu,ModuleList);
                Result.add(menu);
            }

        }
        return  Result;
    }
    public void GetSubMenuList(subMenu subMenu,List<Sys_Module> ModuleList)
    {
        List<subMenu> Result=new ArrayList<>();
        List<Sys_Module> SubModuleList=ModuleList.stream().filter(x->x.Sys_ModuleParentId.equals(subMenu.ModuleId)).collect(Collectors.toList());
        if(SubModuleList.size()>0)
        {
            for (Sys_Module module:
                    SubModuleList) {
                subMenu menu=new subMenu();
                menu.ModuleId=module.Sys_ModuleId;
                menu.url=module.Sys_ModuleUrl;
                menu.name= module.Sys_ModuleName;
                menu.subMenus=new ArrayList<>();
                subMenu.subMenus.add(menu);
            }
        }
    }
    public List<Sys_Module> GetModuleList() throws SQLException, InstantiationException, IllegalAccessException {
        Connection conn = JDBCUtils.findConnection();      //JDBCUtils 自己定义的一个类
        PreparedStatement pre = null;
        ResultSet res = null;
        String sql = "select * from Sys_Module where 1=1 order by Sys_SortNo";
        pre = conn.prepareStatement(sql);
        res = pre.executeQuery();
        //调用将结果集转换成实体对象方法
        List<Sys_Module> list = JDBCUtils.Populate(res,Sys_Module.class);  //.populate(res, TestModel.class);
        //循环遍历结果
        for(int i=0;i<list.size();i++){
            Sys_Module model = (Sys_Module) list.get(i);
        }
        return  list;
    }
    public List<Sys_Module> GetModuleListByRoleId(String RoleId) throws SQLException, InstantiationException, IllegalAccessException {
        String sql="select * from Sys_Module where Sys_ModuleId in (select Sys_ModuleId from Sys_RoleModule where Sys_RoleId='"+RoleId+"')";
        Connection conn = JDBCUtils.findConnection();
        PreparedStatement pre = null;
        ResultSet res = null;
        pre = conn.prepareStatement(sql);
        res = pre.executeQuery();
        //调用将结果集转换成实体对象方法
        List<Sys_Module> list = JDBCUtils.Populate(res,Sys_Module.class);  //.populate(res, TestModel.class);
        //循环遍历结果
        for(int i=0;i<list.size();i++){
            Sys_Module model = (Sys_Module) list.get(i);
        }
        return  list;
    }
}
